package com.zhentao.config;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhentao.domain.Area;
import com.zhentao.domain.City;
import com.zhentao.domain.Province;
import com.zhentao.service.AreaService;
import com.zhentao.service.CityService;
import com.zhentao.service.ProvinceService;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class StartConfig {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AreaService areaService;
    @Autowired
    private CityService cityService;
    @Autowired
    private ProvinceService provinceService;
    @PostConstruct
    void started() {
        String pr = stringRedisTemplate.opsForValue().get("province");
        String ar = stringRedisTemplate.opsForValue().get("area");
        String ci = stringRedisTemplate.opsForValue().get("city");
        if (!StringUtils.isEmpty(pr) && !StringUtils.isEmpty(ar) && !StringUtils.isEmpty(ci)) {
            return;
        }
        List<Province> province = provinceService.list();
        List<Area> area = areaService.list();
        List<City> city = cityService.list();
        stringRedisTemplate.opsForValue().set("province", JSON.toJSONString(province));
        stringRedisTemplate.opsForValue().set("area", JSON.toJSONString(area));
        stringRedisTemplate.opsForValue().set("city", JSON.toJSONString(city));
    }
}
